//
//  ST_GiftRankHeaderView.swift
//  btvideo
//
//  Created by mac on 2020/7/2.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit

class ST_GiftRankHeaderView: UIView {
    
    
    static let height:CGFloat = 47-10 + 213
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .white
        
        self.addSubview(champion_view)
        champion_view.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.centerX.equalToSuperview()
            make.size.equalTo(CGSize.init(width: 105, height: 105+34+44))
        }
        
        self.addSubview(runner_upView)
        runner_upView.snp.makeConstraints { (make) in
            make.bottom.equalTo(champion_view)
            make.right.equalTo(champion_view.snp.left).offset(-TIANCompatibleValue(value: 25))
            make.size.equalTo(CGSize.init(width: 85, height: 85+34+44))
        }
        
        self.addSubview(third_winner_view)
        third_winner_view.snp.makeConstraints { (make) in
            make.bottom.equalTo(champion_view)
            make.left.equalTo(champion_view.snp.right).offset(TIANCompatibleValue(value: 25))
            make.size.equalTo(CGSize.init(width: 85, height: 85+34+44))
        }
        
        self.addSubview(titleLabel)
//        self.addSubview(sweetValueImageView)
//        self.addSubview(sweetLabel)
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.top.equalTo(champion_view.snp.bottom).offset(30)
        }
        
        self.addSubview(infoBtn)
        infoBtn.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-15)
            make.centerY.equalTo(titleLabel)
        }
        
//        sweetLabel.snp.makeConstraints { (make) in
//            make.right.equalToSuperview().offset(-15)
//            make.top.equalToSuperview()
//        }
//        sweetValueImageView.snp.makeConstraints { (make) in
//            make.centerY.equalTo(sweetLabel)
//            make.right.equalTo(sweetLabel.snp.left).offset(-5)
//        }
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = "密友信息"
        control.textColor = UIColor.init(hexString: "#61666F")
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        return control
    }()
    
    
    fileprivate lazy var infoBtn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("亲密等级 ")
        control.setTitleColor(UIColor.init(hex: 0x61666F)!, for: UIControl.State.normal)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        control.setImageForAllStates(ST_ImageUtils.imageName("chat_level_ques")!)
        control.semanticContentAttribute = .forceRightToLeft
        control.addTarget(self, action: #selector(infoHandle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    
    fileprivate lazy var champion_view:ST_GiftRankHeaderSupplementView = {
        let control = ST_GiftRankHeaderSupplementView.init(maxWidth: 105, borderColor: UIColor.init(hex: 0xFEC207)!, topImage: ST_ImageUtils.imageName("champion_of_chumRank_icon")!)
        return control
    }()
    
    fileprivate lazy var runner_upView:ST_GiftRankHeaderSupplementView = {
        let control = ST_GiftRankHeaderSupplementView.init(maxWidth: 85, borderColor: UIColor.init(hex: 0xC8CDD9)!, topImage: ST_ImageUtils.imageName("runner_up_of_chumRank_icon")!)
        return control
    }()
    
    fileprivate lazy var third_winner_view:ST_GiftRankHeaderSupplementView = {
        let control = ST_GiftRankHeaderSupplementView.init(maxWidth: 85, borderColor: UIColor.init(hex: 0xC8CDD9)!, topImage: ST_ImageUtils.imageName("third_winner_of_chumRank_icon")!)
        return control
    }()
    
    var data_array = [ST_GiftRankCellModel](){
        didSet{
         
            if let item = data_array[safe:0]{
                self.champion_view.model = item
                self.champion_view.alpha = 1
            }else{
                self.champion_view.alpha = 0
            }
            
            if let item = data_array[safe:1]{
                self.runner_upView.model = item
                self.runner_upView.alpha = 1
            }else{
                self.runner_upView.alpha = 0
            }
            
            
            if let item = data_array[safe:2]{
                self.third_winner_view.model = item
                self.third_winner_view.alpha = 1
            }else{
                self.third_winner_view.alpha = 0
            }
        }
    }
    
//    fileprivate lazy var sweetValueImageView:UIImageView = {
//        let control = UIImageView.init(image: ST_ImageUtils.imageName("sweet_value_icon"))
//           return control
//       }()
//
//
//    fileprivate lazy var sweetLabel:UILabel = {
//        let control = UILabel.init()
//        control.text = "亲密度"
//        control.textColor = UIColor.init(hexString: "#61666F")
//        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
//        return control
//    }()
}


extension ST_GiftRankHeaderView{
    
    @objc fileprivate func infoHandle(){
     
        self.parentViewController?.navigationController?.pushViewController(ST_SweetRankViewController())
        
    }
}


//MARK: - child View
class ST_GiftRankHeaderSupplementView: UIControl {
    
    convenience init(maxWidth:CGFloat,borderColor:UIColor,topImage:UIImage) {
        self.init()
        
        avatar_image_view.cornerRadius = maxWidth/2.0
        avatar_image_view.borderColor = borderColor
        avatar_image_view.borderWidth = 2
        icon_image_view.borderColor = borderColor
        icon_image_view.borderWidth = 2
        top_image_view.image = topImage
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
     
        self.addSubview(avatar_image_view)
        avatar_image_view.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview().offset(5)
            make.width.equalToSuperview()
            make.centerX.equalToSuperview()
            make.height.equalTo(avatar_image_view.snp.width).dividedBy(1)
        }
        
        self.addSubview(icon_image_view)
        icon_image_view.snp.makeConstraints { (make) in
            make.bottom.right.equalTo(avatar_image_view)
            make.width.height.equalTo(36)
        }
        
        self.addSubview(top_image_view)
        top_image_view.snp.makeConstraints { (make) in
            make.bottom.equalTo(avatar_image_view.snp.top).offset(12)
            make.centerX.equalToSuperview()
            make.width.equalTo(avatar_image_view).dividedBy(2.56)
            make.height.equalTo(top_image_view.snp.width)
        }
        
        self.addSubview(user_name_btn)
        user_name_btn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(avatar_image_view.snp.bottom).offset(5)
            make.height.equalTo(20)
            make.width.lessThanOrEqualToSuperview()
        }
        
        self.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(user_name_btn.snp.bottom).offset(2)
            make.height.equalTo(17)
        }
        
        self.addTarget(self, action: #selector(push_user_center_handle), for: UIControl.Event.touchUpInside)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    fileprivate lazy var avatar_image_view:UIImageView = {
         let control = UIImageView()
          control.contentMode = .scaleAspectFill
          return control
      }()
    
    fileprivate lazy var top_image_view:UIImageView = {
         let control = UIImageView()
          control.contentMode = .scaleAspectFill
          return control
      }()
    
    fileprivate lazy var icon_image_view:UIImageView = {
         let control = UIImageView()
        control.backgroundColor = .white
          control.contentMode = .scaleAspectFill
        control.cornerRadius = 36/2.0
          return control
      }()
    
    
    fileprivate lazy var user_name_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        
        control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium)
        control.setTitleColor(UIColor.init(hex: 0x333333), for: UIControl.State.normal)
        control.titleLabel?.lineBreakMode = .byTruncatingTail
      control.semanticContentAttribute = .forceRightToLeft
        return control
    }()
    
    
    
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = UIColor(hexString: "0xACB4C0")
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        return control
    }()
//    MARK: bind Model
    weak var model:ST_GiftRankCellModel?{
        didSet{
            
            guard let m = model else { return  }
            
            avatar_image_view.pin_setImage(from: m.avatar.compress_small_image(width: 150*3).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            icon_image_view.pin_setImage(from: m.intimacyImg.url)
            
            user_name_btn.setTitleForAllStates(m.nickname + " ")
            
            if m.faceAuditState == 3{
                user_name_btn.setImageForAllStates(ST_ImageUtils.imageName("真人_new_fate")!)
            }else{
                user_name_btn.imageView?.image = nil
            }
            
            content_label.text = "亲密度：" + m.intimacy.string
        }
    }
    
    @objc fileprivate func push_user_center_handle(){
        
        guard let m = model else { return  }
        
        let vc = ST_PairsCenterViewController()
        
        vc.accid = m.accid

        let nav = PairsCenterInteractiveNavigationController.init(rootViewController: vc)
        
        nav.overCurrentContext = true
        
        GlobalPresent(toController: nav)
    }
    
}

